package org.jeecg.modules.controller;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetDeptMemberRequest;
import com.dingtalk.api.response.OapiUserGetDeptMemberResponse;
import com.taobao.api.ApiException;
import org.jeecg.modules.util.AccessTokenUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : ShiJianlong
 * @date : 2020/7/5 08:54
 * 钉钉用户管理
 * 该类里面的方法对应钉钉文档中通讯录管理内的用户管理
 */
@RestController
@RequestMapping("/userManager")
public class UserManagerController {
    /**
     * 获取用户
     * @param deptId
     * @return
     */
    @RequestMapping("/departmentUserIdList/{deptId}")
    public List<String> departmentUserIdList(@PathVariable("deptId") String deptId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
        OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest();
        req.setDeptId(deptId);
        req.setHttpMethod("GET");
        OapiUserGetDeptMemberResponse rsp = null;
        try {
            rsp = client.execute(req, AccessTokenUtil.getToken());
            System.out.println(rsp.getBody());
            return rsp.getUserIds();
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping("/departmentUserIdList")
    public List<String> departmentUserIdList(){
        return departmentUserIdList("1");
    }
}
